﻿
namespace LocationAwareness
{
    public class GPSHandling : GPSInterface
    {
        private Gps gps;

        private GpsPosition position;

        public event GpsDriverInitialized gpsDriverInitialized;
        private bool initialized;

        /// <summary>
        /// Default constructor
        /// </summary>
        public GPSHandling()
        {
            gps = new Gps();
            position = null;

            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);

            initialized = false;
        }

        /// <summary>
        /// Closes the GPS device.
        /// </summary>
        public void closeGPS()
        {
            if (gps.Opened)
                gps.Close();
        }

        /// <summary>
        /// Opens the GPS device.
        /// </summary>
        public void openGPS()
        {
            if (!gps.Opened)
                gps.Open();
        }

        /// <summary>
        /// Method catches an event throw when the location of the GPS device changes.
        /// </summary>
        protected void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            position = args.Position;
            gpsDriverInitialized();
        }


        /// <summary>
        /// Returns the longtitude value of the actual position
        /// </summary>
        public double getLongtitude()
        {
            double d = 0;
            if (position != null)
                d = position.Longitude;
            return d;
        }

        /// <summary>
        /// Returns the latitude value of the actual position
        /// </summary>
        public double getLatitude()
        {
            double d = 0;
            if (position != null)
                d = position.Latitude;
            return d;
        }

        /// <summary>
        /// Returns the actual position
        /// </summary>
        /// <returns>
        /// Actual position as GpsPosition object. 
        /// </returns>
        public GpsPosition getGpsPosition()
        {
            return position;
        }
    }
}
